package com.aglhz.nature.view.weixin;

import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.aglhz.nature.R;
import com.aglhz.nature.view.MainActivity;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.utils.Log;

//微信登陆
public class WeiXinLoginActivity extends Activity{
	private SharedPreferences sharedPreferences;  
	  
    private SharedPreferences.Editor editor;  
	
	private static final String Login_Tag="Login_Tag";
	private UMShareAPI mShareAPI = null;
	String openid;
	String refresh_token;
	String access_token;
	String unionid;
	String nickname;
	String headimgurl;
	Map<String, String> wxinfo;
	  protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.app_auth);	       	      
	        /** init auth api**/
	        mShareAPI = UMShareAPI.get(this);
	        
	        


	  }
	
	 public void onClickAuth(View view) {
         SHARE_MEDIA platform = null;
          if (view.getId() == R.id.app_auth_weixin){
             platform = SHARE_MEDIA.WEIXIN;
         }
           
         /**begin invoke umeng api**/

         mShareAPI.doOauthVerify(WeiXinLoginActivity.this, platform, umAuthListener);
     }

	 
	 
	 /** auth callback interface**/
	    private UMAuthListener umAuthListener = new UMAuthListener() {
	        @Override
	        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
	        	//获取access_token
	        	openid=data.get("openid");
	        	refresh_token=data.get("refresh_token");
	        	access_token=data.get("access_token");
	        	unionid=data.get("unionid");
//	        	Log.e("access_token","----------------------"+openid+refresh_token);
//	        	Log.e("access_token","----------------------"+data.toString());
	        	mShareAPI.getPlatformInfo(WeiXinLoginActivity.this, platform, umAuthUserListener);
	          
	            

	            
	        }

	        @Override
	        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
//	            Toast.makeText( getApplicationContext(), "Authorize fail", Toast.LENGTH_SHORT).show();
	        }

	        @Override
	        public void onCancel(SHARE_MEDIA platform, int action) {
//	            Toast.makeText( getApplicationContext(), "Authorize cancel", Toast.LENGTH_SHORT).show();
	        }
	    };
	  
	    
	   //用户信息 
	    private UMAuthListener umAuthUserListener = new UMAuthListener() {
	        @Override
	        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {

	            if (data!=null){
	                Log.d("auth callbacl","getting data");
	              
	               //获取用户信息
	                Log.e("获取用户信息", "------------------"+data.toString());
		            Intent intent =new Intent(WeiXinLoginActivity.this,MainActivity.class);		            
		           intent.putExtra("openid",openid);
		           intent.putExtra("refresh_token",refresh_token);
		           intent.putExtra("Login_Tag",Login_Tag);
		           
		           nickname=data.get("nickname");
		           headimgurl=data.get("headimgurl");
		           //应该保存
			        sharedPreferences = getSharedPreferences("WX_INFO",Context.MODE_PRIVATE);  
			           editor = sharedPreferences.edit();  
			           editor.putString("refresh_token",refresh_token);
			           editor.putString("openid",openid);
			           editor.putString("access_token",access_token);
			           editor.putString("nickname",nickname);
			           editor.putString("headimgurl",headimgurl);
			           editor.putString("unionid",unionid);
			           // 一定要提交  
			           Log.e("sharedPreferences","------------");
		                editor.commit();  
		           
//		           Log.e("tttt","--------------------"+refresh_token+"--------------------"+openid);
		            startActivity(intent);
		            finish();
	            }
	        }

	        @Override
	        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
//	            Toast.makeText( getApplicationContext(), "get fail", Toast.LENGTH_SHORT).show();
	        }

	        @Override
	        public void onCancel(SHARE_MEDIA platform, int action) {
//	            Toast.makeText( getApplicationContext(), "get cancel", Toast.LENGTH_SHORT).show();
	        }
	    };

	    @Override
	    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	        super.onActivityResult(requestCode, resultCode, data);
	        Log.d("auth", "on activity re 2");
	        mShareAPI.onActivityResult(requestCode, resultCode, data);
	        Log.d("auth", "on activity re 3");
	    }
}
